package com.example.combwx.network.interceptor;

import android.util.Log;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 针对接口中token限定的拦截器,
 * 像每个接口中的请求头添加token
 * */
public class TokenInterceptor implements Interceptor {
    private static final String TAG = "xrn";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Log.i(TAG, "intercept: ");
        //先获取原有Request
        Request oldRequest = chain.request();
        //配置添加过token的Request
        Request.Builder builder = new Request.Builder();
        builder.url(oldRequest.url());
        builder.headers(oldRequest.headers());
        builder.addHeader("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI4NjMyNTQ3NTI5NjAzMjciLCJleHAiOjMyNDI2NDA1MjUsImlhdCI6MTYyMTMyMDI2Mn0.6pFz9_n_hXUVS75Dk5m3raJgeMhDC6qfLEp4h4IW-9c");
        builder.post(oldRequest.body());
        Request request = builder.build();
        return chain.proceed(request);
    }
}
